/* data */
import BizListViewTaskData from '@src/component/business/BizListView/Impl/BizListViewTask/BizListViewTaskData'
/* enum */
import AuthEnum from '@model/enum/AuthEnum'
import AuthValueEnum from '@model/enum/AuthValueEnum'
import { FieldTypeMappingEnum, TaskFieldNameMappingEnum } from '@model/enum/FieldMappingEnum'
/* entity */
import Field from '@model/entity/Field'
import TaskType from '@model/entity/TaskType'
/* model */
import SystemFieldSetting from '@model/types/SystemFieldSetting'
/* service */
import { existsField, getFieldWithFieldName, isEnabled } from '@service/FieldService'
/* util */
import { taskTypeSelectConversion } from '@src/util/conversionFunctionUtil'
import { isNaN } from '@src/util/validate/number'
import * as _ from 'lodash'
import { isArray } from '@src/util/type'
import { isBasicEditionHideProduct, isBasicEditionHidePrintTask, isBasicEditionHidePay } from '@shb-lib/version'

class BizListViewTaskComputed<T, P> extends BizListViewTaskData<T, P> {
  
  /**
   * @description: 是否允许删除
   * @return {boolean}
   */
  get allowDelete(): boolean {
    return this.allotEditAll
  }
  
  /**
   * @description: 是否允许编辑全部
   * @return {boolean}
   */
  get allotEditAll(): boolean {
    return Boolean(this.auth?.[AuthEnum.TASK_EDIT] === AuthValueEnum.All)
  }
  
  /**
   * @description: 是否允许添加工单
   * @return {boolean}
   */
  get allowTaskAdd(): boolean {
    return Boolean(this.auth?.[AuthEnum.TASK_ADD])
  }
  
  /**
   * @description: 是否允许查看工单详情
   * @return {boolean}
   */
  get allowTaskViewDetail(): boolean {
    return Boolean(this.auth?.[AuthEnum.TASK_VIEW_DETAIL])
  }
  
  /**
   * @description: 客户字段
   * @return {Field | null}
   */
  get customerField(): Field | null {
    // 客户字段
    return getFieldWithFieldName(this.taskFields || [], TaskFieldNameMappingEnum.Customer)
  }
  
  get descriptionField(): Field | null {
    return existsField(TaskFieldNameMappingEnum.Description, undefined, this.taskFieldsMap)
  }
  
  get levelField(): Field | null {
    return existsField(TaskFieldNameMappingEnum.Level, undefined, this.taskFieldsMap)
  }
  
  get serviceTypeField(): Field | null {
    return existsField(TaskFieldNameMappingEnum.ServiceType, undefined, this.taskFieldsMap)
  }
  
  get serviceContentField(): Field | null {
    return existsField(TaskFieldNameMappingEnum.ServiceContent, undefined, this.taskFieldsMap)
  }
  
  /**
   * @description: 系统字段设置
   * @return {SystemFieldSetting}
   */
  get systemFieldSetting(): SystemFieldSetting {
    return {
      hasServiceType: Boolean(this.serviceTypeField && isEnabled(this.serviceTypeField)),
      hasServiceContent: Boolean(this.serviceContentField && isEnabled(this.serviceContentField)),
      hasLevel: Boolean(this.levelField && isEnabled(this.levelField)),
      hasDescription: Boolean(this.descriptionField && isEnabled(this.descriptionField))
    }
  }
  
  /**
   * @description: 工单类型列表
   * @return {TaskType[]}
   */  
  get taskTypeList(): TaskType[] {
    const taskTypeList: TaskType[] = [
      { name: '全部', templateId: '', id: '' },
      ...this.taskTypes
    ]
    
    return taskTypeList.map(taskType => taskTypeSelectConversion(taskType))
  }
  
  get taskListFields(): Field[] {
    // 工单固定字段
    const columnsField = _.cloneDeep(this.columns)
    const FixedFields = columnsField.slice() as unknown as Field[]
    // const FixedFields = fields.slice() as unknown as Field[]
    let sortResult: number
    
    return (
      FixedFields
        .filter(field => field.formType !== FieldTypeMappingEnum.Separator && field.formType !== FieldTypeMappingEnum.Info)
        .sort((a, b) => {
          sortResult = Number(a.orderId) - Number(b.orderId)
          return isNaN(sortResult) ? -1 : sortResult
        })
    )
  }

  // 基础版是否隐藏产品
  get isBasicEditionHideProduct(): boolean {
    return isBasicEditionHideProduct() 
  }

  // 基础版功能是否隐藏打印
  get isBasicEditionHidePrintTask(): boolean {
    return isBasicEditionHidePrintTask() 
  }

  // 基础版功能是否隐藏支付
  get isBasicEditionHidePay(): boolean {
    return isBasicEditionHidePay() 
  }

  get taskTypeColumnsStorageKey() {
    const key = this.currentTaskType?.id ? `${this.selectColumnStorageKey}_${this.currentTaskType.id}` : this.selectColumnStorageKey;
    return key
  }

  get taskTypeExportStorageKey() {
    const key = this.currentTaskType?.id ? `${this.exportStorageKey}_${this.currentTaskType.id}` : this.exportStorageKey;
    return key
  }
}

export default BizListViewTaskComputed

